//
//  CYMyReleaseDetailTVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/7/31.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYMyReleaseDetailTVC.h"
#import "CYDefaultRecord.h"
#import "CYCreditTool.h"
#import "CYLoginHttpTool.h"
#import "CYEscape.h"
#import "UIImageView+WebCache.h"
#import "CYDefaultRecordPics.h"
#import "CYImageScrollView.h"
#import "MBProgressHUD+MJ.h"

@interface CYMyReleaseDetailTVC ()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *idCardLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *registeredAddressLabel;
@property (weak, nonatomic) IBOutlet UILabel *lendLabel;
@property (weak, nonatomic) IBOutlet UILabel *lendTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *shouldPayTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *payTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *describeLabel;
@property (weak, nonatomic) IBOutlet UITextView *commentTF;
@property (weak, nonatomic) IBOutlet UIImageView *idCardZIV;
@property (weak, nonatomic) IBOutlet UIImageView *idCardFIV;
@property (weak, nonatomic) IBOutlet CYImageScrollView *compactSV;
@property (weak, nonatomic) IBOutlet CYImageScrollView *receiptSV;
@property (weak, nonatomic) IBOutlet CYImageScrollView *otherSV;

@property(nonatomic, strong) CYDefaultRecord *showData;
@end

@implementation CYMyReleaseDetailTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
    
    [self loadDetail];
}

- (void)loadDetail {
    CYUser *user = [CYLoginHttpTool userData].user;
    [MBProgressHUD showMessage:@"加载中" toView:self.view];
    [CYCreditTool editDefaultRecordWithParams:@{@"id": self.record.defaultRecordId, @"userId": user.userId, @"token": user.token} success:^(CYDefaultRecord *defr) {
        [MBProgressHUD hideHUDForView:self.view];
        self.showData = defr;
        [self setupData];
        //加载图片
        [self loadImage];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
    }];
}

- (void)setupData {
    CYUser *user = [CYLoginHttpTool userData].user;
    
    self.nameLabel.text = CYStringFormat(@"姓名：%@", self.showData.name);
    self.phoneLabel.text = CYStringFormat(@"电话：%@", self.showData.phone);
    self.idCardLabel.text = CYStringFormat(@"身份证号：%@", self.showData.certificateNo);
    self.addressLabel.text = CYStringFormat(@"家庭住址：%@", self.showData.homeAddress ? self.showData.homeAddress:@"");
    self.registeredAddressLabel.text  = CYStringFormat(@"户籍地址：%@", self.showData.registerAddress ? self.record.registerAddress: @"");
    self.lendLabel.text = CYStringFormat(@"借款金额：%@", self.showData.amountPrice);
    NSString *lendTime = [CYCreditTool timewithLongLongValue:self.showData.lendTime.longLongValue formatter:nil];
    self.lendTimeLabel.text  = CYStringFormat(@"借款时间：%@", lendTime);
    NSString *startTime = [CYCreditTool timewithLongLongValue:self.showData.startTime.longLongValue formatter:nil];
    self.shouldPayTimeLabel.text  = CYStringFormat(@"本应还款时间：%@", startTime);
    if (!self.showData.endTime) {
        self.payTimeLabel.text  = CYStringFormat(@"最终还款时间：%@", @"逾期未还");
    } else {
        NSString *end = [CYCreditTool timewithLongLongValue:self.showData.endTime.longLongValue formatter:nil];
        self.payTimeLabel.text  = CYStringFormat(@"最终还款时间：%@", end);
    }
    self.describeLabel.text  = [NSString stringWithFormat:@"       %@于%@向%@进行贷款%@元，本应于%@之前进行还款，但至今仍未履行还款义务，逾期%@天，现对%@进行网络公示，望引起违约人的重视。\n\n", self.showData.name, [CYCreditTool timewithLongLongValue:[self.showData.lendTime longLongValue] formatter:nil], user.name, self.showData.amountPrice, [CYCreditTool timewithLongLongValue:[self.showData.startTime longLongValue] formatter:nil], [CYCreditTool dayWithStartValue:[NSNumber numberWithLongLong:self.showData.startTime.longLongValue] endValue:[NSNumber numberWithLongLong:self.showData.endTime.longLongValue]], self.showData.name];
    
    self.commentTF.text  = CYStringFormat(@"%@", self.showData.memo ? self.showData.memo: @"");
}

- (void)loadImage {
    CYUser *user = [CYLoginHttpTool userData].user;
    [CYCreditTool defaultPicsWithParams:@{@"defaultId": self.record.defaultRecordId, @"userId": user.userId, @"token": user.token} success:^(CYDefaultRecordPics *defrs) {
        if (defrs.idCardZImageUrl) {
            [self.idCardZIV sd_setImageWithURL:CYURLWithService(defrs.idCardZImageUrl) placeholderImage:[UIImage imageNamed:@"putao_icon_quick_replace_s"]];
        }
        if (defrs.idCardFImageUrl) {
            [self.idCardFIV sd_setImageWithURL:CYURLWithService(defrs.idCardFImageUrl) placeholderImage:[UIImage imageNamed:@"putao_icon_quick_replace_s"]];
        }
        
        self.compactSV.imageUrls = defrs.compactImageUrls;
        self.receiptSV.imageUrls = defrs.certificateUrls;
        self.otherSV.imageUrls = defrs.otherImageUrls;
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - Table view delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.showData) {
        return 9;
    }
    return 0;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (void)dealloc {
    CYLog(@"通缉详情页销毁");
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
